
	footer.init();
	header.init('设置');
	//头像
	app.ajax(
		'/Member/Setting/index',
		null,
		function(res){
			if(res.code == 200){
				app.tpl($('.setPer'),'tpl-setPer',res);
			}else{
				layer.msg('网络繁忙,请稍候再试');
			}
		}
	);

window.onload = function(){
	
	$('body').on('click','.uName',function(){
		$(this).find('input').focus();
		$(this).find('input').attr('placeholder','');
		
	});
	//修改昵称
	var uName = $(".uName input").val();
	$('body').on('blur','.uName input',function(){
		if(uName == $(this).val()) return false;
		if(uName == ''){
			layer.msg('昵称不能为空');
			$(this).focus();
			return false;
		}else if( uName.length > 10){
			layer.msg('昵称长度不能超过10位');
			$(this).focus();
			return false;
		}
		uName == $(this).val();
		app.ajax(
			'/Member/Setting/update_name',
			{
				name : uName
			},function(res){
				if(res.code == 200){
					layer.msg('修改昵称成功！');
				}else{
					layer.msg(res.msg);
				}
			}
		);
		
	});
	$('body').on('click','.weChatNum',function(){
		$(this).find('input').focus();
		$(this).find('input').attr('placeholder','');
	});
	
	$('body').on('click','.tel',function(){
		var tel = $(this).attr('data-tel');
//		if(tel){
//			layer.msg('请联系客服，修改手机号');
//			return false;
//		}
//		app.open('user.modifyTel.html','modifyTel',{},window.location.href,document.body.scrollTop);
		app.open('user.bindNewTel.html','bindNewTel',{},window.location.href,document.body.scrollTop);
	});
	
	$('body').on('click','.addr',function(){
		app.open('user.manAddr.html','manAddr',{},window.location.href,document.body.scrollTop);
	});
	
	$('body').on('click','.setPwd',function(){
		var tel = $('.tel').attr('data-tel');
		if(!tel){
			layer.msg('请先设置手机号');
			return false;
		}
		app.open('user.setPayPwd.html','payPwd',{tel : tel},window.location.href,document.body.scrollTop);
	});
	
	$('body').on('click','.portrait .avatar .shopLogoImg',function(){
		$("input#upload_file").trigger("click");
	});
	
	$("body").on("change", "input#upload_file", function(){
		var _this = this;
		var oFile = $(_this)[0].files[0];	
		var rFilter = /^(image\/jpeg|image\/png)$/i;
	    if (! rFilter.test(oFile.type)) {
	        layer.open({content:"文件格式不对",time:2,shadeClose:false});
	        return;
	    }
	    var oReader = new FileReader();
	    oReader.readAsDataURL(oFile);
	    oReader.onload = function(e) {
	    	var result = this.result;
            // 如果图片大于1M，则不能上传
	    	var maxsize = 1024*1024;
            if (e.total > maxsize) {
            	layer.open({content:'图片大小只能1M以下',time:2,shadeClose:false});
            	return false;
            }
            var img = new Image();
            img.src = result;
            if (img.complete) {
                callback();
            } else {
                img.onload = callback;
            }
            function callback() {
                var base64data = compress(img);
                img = null;
                // 上传到服务器
				$.ajax({
					url:"http://img.52ywy.com/upload?from=base64&act=userLogo",
					type:"post",
					dataType:"json",
					data:{
						imageData:base64data
					},
					error:function (e) {
						console.log(e);
					},
					success:function (res) {
						if(res.code == "0"){
							$("#userImg").attr('src', res.data[0]);
							$.ajax({
								url:"http://webapi.52ywy.com/App/Member/Setting/upload",
								type:"post",
								dataType:"json",
								data:{
									logo:res.data[0]
								},
								error:function (e) {
                                    console.log(e);
								},
								success:function (data) {
									console.log(data);
								}
							});
						}
					}
				});
            }
	    };
	});
};

function compress(img) {
	// 用于压缩图片的canvas
	var canvas = document.createElement("canvas");
	var ctx = canvas.getContext('2d');
	// 瓦片canvas
	var tCanvas = document.createElement("canvas");
	var tctx = tCanvas.getContext("2d");
	
	var initSize = img.src.length;
	var width = img.width;
	var height = img.height;
	//如果图片大于四百万像素，计算压缩比并将大小压至400万以下
	var ratio;
	if ((ratio = width * height / 4000000)>1) {
		ratio = Math.sqrt(ratio);
		width /= ratio;
		height /= ratio;
	}else {
		ratio = 1;
	}
	canvas.width = width;
	canvas.height = height;
	// 铺底色
	ctx.fillStyle = "#fff";
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	// 如果图片像素大于100万则使用瓦片绘制
	var count;
	if ((count = width * height / 1000000) > 1) {
		count = ~~(Math.sqrt(count)+1); //计算要分成多少块瓦片
		// 计算每块瓦片的宽和高git 
		var nw = ~~(width / count);
		var nh = ~~(height / count);
		tCanvas.width = nw;
		tCanvas.height = nh;
		for (var i = 0; i < count; i++) {
			for (var j = 0; j < count; j++) {
				tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
				ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
    		}
      	}
  	} else {
  		ctx.drawImage(img, 0, 0, width, height);
  	}
  	//进行最小压缩
  	var ndata = canvas.toDataURL('image/jpeg', 0.1);
  	console.log('压缩前：' + initSize);
  	console.log('压缩后：' + ndata.length);
  	console.log('压缩率：' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
  	tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
  	return ndata;
}

function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB'];
    if (bytes == 0) return 'n/a';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};